package com.example.springbatch_sing_stop;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchApplication12 {


    @Resource
    private JobBuilderFactory jobBuilderFactory;

    @Resource
    private StepBuilderFactory stepBuilderFactory;

    private int readCount = 100; //模拟只读取50个

    @Bean
    public Tasklet taskletA() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step1执行了.......");
                for (int i = 1; i <= readCount; i++) {
                    ResouceCount.readCount++;
                }
                // 使用签名的方式停止
                // 其中的StepExecution的setTerminateOnly() 给运行中的stepExecution设置停止标记，Spring Batch 识别后直接停止步骤，进而停止流程
                if(ResouceCount.readCount != ResouceCount.totalCount){
                    chunkContext.getStepContext().getStepExecution().setTerminateOnly();
                }
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet taskletB() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.err.println("step2执行了.......");
                System.err.println("readCount:" + ResouceCount.readCount + ", totalCount:" + ResouceCount.totalCount);
                return RepeatStatus.FINISHED;
            }
        };
    }



    @Bean
    public Step stepA() {
        return stepBuilderFactory.get("stepA").tasklet(taskletA())
                .allowStartIfComplete(true)  //允许重启后重新执行
                .build();
    }


    @Bean
    public Step stepB() {
        return stepBuilderFactory.get("stepB").tasklet(taskletB()).build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("step-stop-job-sign-3").start(stepA())
                .on("STOPPED").stopAndRestart(stepA())  //暂停后从stepA开始执行
                .from(stepA()).on("*").to(stepB()).end().build();
    }


    public static void main(String[] args) {
        SpringApplication.run(SpringBatchApplication12.class);
    }


}
